﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace iDMS.Models
{
    public class DMSRepository : IDMSRepository
    {
        iDMSDataClassesDataContext db = new iDMSDataClassesDataContext();

        #region DesignChange
        public IQueryable<DesignChange> DesignChangesGetAll()
        {
            return db.DesignChanges;

        }


        public IQueryable<DesignChangePhase> PhasesGetAll()
        {
            return db.DesignChangePhases;
        }

        public IQueryable<DesignChangeReason> ReasonsGetAll()
        {
            return db.DesignChangeReasons;
        }

        public IQueryable<DesignChangeLog> DesignChangesLogsGetAll()
        {
            return db.DesignChangeLogs;
        }

        public void DesignChangeAdd(DesignChange dc)
        {
            db.DesignChanges.InsertOnSubmit(dc);
        }

        public IQueryable<WorkflowStep> WorkflowStepsGetAll()
        {
            return db.WorkflowSteps;
        }



        //=>Class Helper
        public WorkflowStep WorkflowStepFind(byte oldPhase, byte newPhase)
        {
            return db.WorkflowSteps.SingleOrDefault(d => d.PhaseFrom == oldPhase && d.PhaseTo == newPhase);
        }

        public IQueryable<Note> NotesGetAll()
        {
            return db.Notes;
        }

        public void NoteAdd(Note note)
        {
            db.Notes.InsertOnSubmit(note);
        }

        public void DesignChangeLogAdd(DesignChangeLog entity)
        {
            db.DesignChangeLogs.InsertOnSubmit(entity);
        }

        #endregion

        public void CommitChanges()
        {
            db.SubmitChanges();
        }

        #region Drawing
        public IQueryable<Drawing> DrawingGetAll()
        {
            return db.Drawings;
        }

        public IQueryable<Sheet> SheetsGetAll()
        {
            return db.Sheets;
        }

        public IQueryable<SheetFormat> SheetFormatGetAll()
        {
            return db.SheetFormats;
        }


        public IQueryable<SheetAttachment> SheetAttachmentGetAll()
        {
            return db.SheetAttachments;
        }

        public IQueryable<Resource> ResourcesGetAll()
        {
            return db.Resources;
        }

        public IQueryable<InformationCarrier> InformationCarriersGetAll()
        {
            return db.InformationCarriers;
        }

        public IQueryable<Unit> UnitsGetAll()
        {
            return db.Units;
        }


        public void DrawingAdd(Drawing entity)
        {
            db.Drawings.InsertOnSubmit(entity);
        }

        public void DrawingDelete(Drawing entity)
        {
            List<Sheet> sheets = entity.Sheets.ToList();
            foreach (Sheet item in sheets)
            {
                SheetDelete(item);
            }
            db.Drawings.DeleteOnSubmit(entity);
        }

        public void SheetAdd(Sheet entity)
        {
            db.Sheets.InsertOnSubmit(entity);
        }

        public void SheetDelete(Sheet entity)
        {
            if (entity.Revision > 0)
            {
                Sheet previousVersion = SheetsGetAll().Where(s => (s.DrawingID == entity.DrawingID) && (s.PageNmb == entity.PageNmb) && (s.Revision == entity.Revision - 1)).SingleOrDefault();
                if (previousVersion != null)
                {
                    previousVersion.InnaplicableChangeID = null;
                    previousVersion.UnderRevisionWithdrawal = null;
                }
            }
            Resource resource = entity.Resource;
            List<SheetAttachment> attachments = new List<SheetAttachment>();

            attachments = SheetAttachmentGetAll().Where(d => d.SheetID == entity.ScheetID).ToList();
            if (attachments.Any())
            {
                foreach (SheetAttachment item in attachments)
                {
                    Resource rsx = item.Resource;
                    db.SheetAttachments.DeleteOnSubmit(item);
                    db.Resources.DeleteOnSubmit(rsx);
                } 
            }
            db.Sheets.DeleteOnSubmit(entity);
            if (resource !=null)
            {
                db.Resources.DeleteOnSubmit(resource); 
            }
        }

        public void ResourceAdd(Resource entity)
        {
            db.Resources.InsertOnSubmit(entity);
        }


        public void ResourceDelete(Resource entity)
        {
            db.Resources.DeleteOnSubmit(entity);
        }

        public void SheetAttachmentAdd(SheetAttachment entity)
        {
            db.SheetAttachments.InsertOnSubmit(entity);
        }

        public void SheetAttachmentDelete(SheetAttachment entity)
        {
            db.SheetAttachments.DeleteOnSubmit(entity);
        }


        #endregion

        #region Manufacturer
        public IQueryable<Manufacturer> ManufacturersGetAll()
        {
            return db.Manufacturers;
        }


        public IQueryable<Country> CountriesGetAll()
        {
            return db.Countries;
        }



        public void ManufacturerAdd(Manufacturer entity)
        {
            db.Manufacturers.InsertOnSubmit(entity);
        }


        #endregion

        #region Employee
        public IQueryable<Employee> EmployeesGetAll()
        {
            return db.Employees;
        }

        public void EmployeeAdd(Employee entity)
        {
            db.Employees.InsertOnSubmit(entity);
        }

        public void DepartmentsAdd(Department entity)
        {
            db.Departments.InsertOnSubmit(entity);
        }

        public void DepartmentsDelete(Department entity)
        {
            db.Departments.DeleteOnSubmit(entity);
        }

        public IQueryable<Department> DepartmentsGetAll()
        {
            return db.Departments;
        }

        #endregion

        public IQueryable<SoftwarePackage> SoftwarePackagesGetAll()
        {
            return db.SoftwarePackages;
        }

    }
}